package com.daro.ddlaunch.controller;

import com.daro.ddlaunch.pojo.Launchevent;
import com.daro.ddlaunch.pojo.Parameter;
import com.daro.ddlaunch.pojo.Position;
import com.daro.ddlaunch.service.IndexService;
import com.daro.ddlaunch.utils.Utils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
public class IndexController {

    @Autowired
    public IndexService indexService;

    public static Log log = LogFactory.getLog(IndexController.class);
    @RequestMapping("/index")
    public String index(){
        return "index";
    }

    @PostMapping("/getParameter")
    @ResponseBody
    public String req(@RequestParam(value = "ids") Integer ids,HttpServletRequest request) {
        String Message = "正在检查";
        System.out.println(ids);
        HttpSession session = request.getSession();
        int issuccessful = 1;
        if(ids < 70){
            issuccessful = 0;
            log.info("检查通过可以发射");
            Message = "可以发射";
        }else{
            log.info("检查不通过不可以发射");
            Message = "不可以发射";
        }

        Launchevent launchevent = new Launchevent();
        SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        launchevent.setLaunchtime(sd.format(new Date()));
        launchevent.setIssuccessful(issuccessful);
        launchevent.setLaunchlocation("116.841567,39.574812");
        launchevent.setLaunchsuccessfully(1);
        Integer launchid = indexService.insertLaunchevent(launchevent);
        Integer launchid1 = launchevent.getLaunchid();
        session.setAttribute("launchid",launchid1);
        return Message;
    }


    @RequestMapping("/geturl")
    @ResponseBody
    public String geturl(@RequestParam(value = "ids") Integer ids ,HttpServletRequest request) {
        String Message = "no";
        System.out.println(ids);
        Object launchid = request.getSession().getAttribute("launchid");
        if(ids < 70){
            log.info("发射成功");
            Message = "http://localhost:8088/";
        }
        return Message;
    }


    @RequestMapping("/getlaunch")
    public void launch(@RequestParam(value = "par") String par ,HttpServletRequest request) {
        Object launchid = request.getSession().getAttribute("launchid");
        String[] arr = par.split(",");
        String longitude = arr[0];
        String latitude = arr[1];
        String targetlocation = longitude + "," + latitude;
        String temperature = arr[2];
        String speed = arr[3];
        indexService.UpdateLaunchsuccessfully(targetlocation,temperature,speed,launchid);

    }


}
